package dao.Impl;

import context.BaseContext;
import dao.BaseDao;
import dao.UserDao;
import model.VO.UserLoginVO;
import model.entity.User;
import util.NowTime;

import java.sql.SQLException;
import java.sql.Timestamp;

public class UserDaoImpl extends BaseDao implements UserDao {
    //用户登录
    @Override
    public UserLoginVO login(String username, String password) {
        String oldPassword="";
        UserLoginVO ul=new UserLoginVO();
        String sql="select password,id,name,avatar,phone from user where username=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,username);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while(rs.next()){
                oldPassword=rs.getString("password");
                if(password.equals(oldPassword)){
                    ul.setId(rs.getInt("id"));
                    ul.setName(rs.getString("name"));
                    ul.setAvatar(rs.getString("avatar"));
                    ul.setPhone(rs.getString("phone"));
                    System.out.println("用户登陆成功");
                }
                else {
                    System.out.println("用户登陆失败");
                    ul.setName(null);
                }
            }

        } catch (SQLException e) {
            System.out.println("用户登陆失败"+e);
        }
        return ul;
    }

    //查询数据库中是否已有username
    @Override
    public int find(String username) {
        String sql="select username from user where username=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,username);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            if (rs.next()) {
                // 结果集不为空，说明查询到了数据
                System.out.println("查询成功，有重复账号");
                return 1;
            } else {
                // 结果集为空，说明没有查询到数据
                System.out.println("查询成功，没有重复账号");
                return 0;
            }
        } catch (SQLException e) {
            System.out.println("查询失败"+e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void register(User u) {
        NowTime nowTime = new NowTime();
        String t = nowTime.getTime();
        String sql="insert into user (username, password, name, phone, gender, avatar, create_time) VALUES (?,?,?,?,?,?,?);";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,u.getUsername());
            pstmt.setString(2,u.getPassword());
            pstmt.setString(3, u.getName());
            pstmt.setString(4,u.getPhone());
            pstmt.setString(5,u.getGender());
            pstmt.setString(6,u.getAvatar());
            pstmt.setTimestamp(7, Timestamp.valueOf(t));
            pstmt.executeUpdate();
            System.out.println("用户注册成功");
        } catch (SQLException e) {
            System.out.println("注册用户失败"+e);
        }
    }

    @Override
    public int getAll() {
        String sql="select count(id) as `totalUsers` from user;";
        int totalUsers=0;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                totalUsers=rs.getInt("totalUsers");
            }
        } catch (SQLException e) {
            System.out.println("获取所有用户失败："+e);
        }
        return totalUsers;
    }

    @Override
    public int getDayUsers() {
        String sql="select count(id) as `newUsers` from user where DATE (create_time)=curdate();";
        int newUsers=0;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                newUsers=rs.getInt("newUsers");
            }
        } catch (SQLException e) {
            System.out.println("获取今日用户数失败："+e);
        }
        return newUsers;
    }

    @Override
    public User getUserById(int id) {
        User u=new User();
        String sql="select * from user where id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                u.setId(rs.getInt("id"));
                u.setAvatar(rs.getString("avatar"));
                u.setGender(rs.getString("gender"));
                u.setName(rs.getString("name"));
                u.setUsername(rs.getString("username"));
                u.setPhone(rs.getString("phone"));
                u.setPassword(rs.getString("password"));
                u.setCreatTime(String.valueOf(rs.getTimestamp("create_time")));
            }
            System.out.println("获取当前用户信息成功!");
        }
        catch (SQLException e){
            System.out.println("获取当前用户信息失败!"+e);
        }
        return u;
    }
}
